package com.ruicar.afs.cloud.apply.business.service.impl;

import com.ruicar.afs.cloud.apply.business.service.ApplySequenceService;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.common.core.uid.AfsSequenceGenerator;
import com.ruicar.afs.cloud.common.data.sequence.entity.AfsSequenceInfo;
import com.ruicar.afs.cloud.common.data.sequence.service.AfsSequenceService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @description: 申请进件序列号
 * @author: sijun.yu
 * @date: 2020/8/16 15:35
 */
@AllArgsConstructor
@Service
public class ApplySequenceServiceImpl implements ApplySequenceService {

    private AfsSequenceGenerator afsSequenceGenerator;
    private AfsSequenceService afsSequenceService;

    /**
     * 申请编号-正式编号
     * @param businessType
     * @return
     */
    public String getApplyNo(String businessType) {

        String applyNo = "";
        String typeIndex = ApplyConstants.VALUE_INDEX_APPLY_NO;
        // 新车
        if (ApplyConstants.BUSINESS_TYPE_NEW_CAR.equals(businessType)) {

            String letter = getLetterNext(ApplyConstants.VALUE_NO_DP_APPLY_NO);
            Long seqno = afsSequenceGenerator.genNext(letter, typeIndex);
            applyNo = letter + StringUtils.leftPad(String.valueOf(seqno), 6, '0');// 申请编号
        }
        // 二手车
        else if (ApplyConstants.BUSINESS_TYPE_OLD_CAR.equals(businessType)) {
            String letter = getLetterNext(ApplyConstants.VALUE_NO_ER_APPLY_NO);
            Long seqno = afsSequenceGenerator.genNext(letter, typeIndex);
            applyNo = letter + StringUtils.leftPad(String.valueOf(seqno), 6, '0');// 申请编号
        }
        // 本品
        else {
            String letter = getLetterNext(ApplyConstants.VALUE_NO_BP_APPLY_NO);
            Long seqno = afsSequenceGenerator.genNext(letter, typeIndex);
            applyNo = letter + StringUtils.leftPad(String.valueOf(seqno), 6, '0');// 申请编号
        }
        return applyNo;
    }

    /**
     * 申请编号-临时编号
     * @return
     */
    public String getTempNoForApply(){

        String letter = getLetterNext(ApplyConstants.VALUE_NO_TP_APPLY_NO);
        Long seqno = afsSequenceGenerator.genNext(letter, ApplyConstants.VALUE_INDEX_APPLY_NO);
        String tempNo = letter + StringUtils.leftPad(String.valueOf(seqno), 6, '0');// 申请编号
        return tempNo;
    }

    /**
     * 获取下一个字母
     * @param keyType
     * @return
     */
    public String getLetterNext(String keyType){

        int serNo = ApplyConstants.INIT_LETTER_NUMBER;
        AfsSequenceInfo letterInfo = afsSequenceService.query(keyType,ApplyConstants.VALUE_INDEX_LETTER);
        if(letterInfo != null){
            serNo = letterInfo.getLastValue().intValue() + 1;
        }

        AfsSequenceInfo applyInfo = afsSequenceService.query(keyType + "-" + ApplyConstants.LETTER.get(serNo),ApplyConstants.VALUE_INDEX_APPLY_NO);
        if(applyInfo != null){
            if(ApplyConstants.MAX_COUNT <= applyInfo.getLastValue()){
                serNo = afsSequenceGenerator.genNext(keyType,ApplyConstants.VALUE_INDEX_LETTER).intValue() + 1;
            }
        }
        return keyType + "-" + ApplyConstants.LETTER.get(serNo);
    }

}
